<?php
/**
 * Created by PhpStorm.
 * User: surface
 * Date: 2018/12/15
 * Time: 15:02
 */

namespace app\api\controller;

use app\common\model\Room as RoomModel;
use app\common\model\RoomBooking as RoomBookingModel;
use app\common\controller\Api;
use Request;
use app\common\model\Setting as SettingModel;

class Room extends Api
{

    public function roomList()
    {
        $result = [
            'status' => true,
            'msg' => '获取成功',
            'data' => []
        ];
        $field = 'id,name,bn,price,brief,sort';
        $order = 'sort asc';
        $roomModel = new RoomModel();
        $list = $roomModel->field($field)->order($order)->select();

        if (!$list->isEmpty()) {
            $roomBookingModel = new RoomBookingModel;
            $hour=date("G");
            if($hour < 3){
                $begintime=strtotime(date("Y-m-d",strtotime("-1 day"))." 0:0:0");
                $endtime=strtotime(date("Y-m-d",strtotime("-1 day"))." 23:59:59");
            }else{
                $begintime=strtotime(date("Y-m-d",time())." 0:0:0");
                $endtime=strtotime(date("Y-m-d",time())." 23:59:59");
            }

            foreach ($list as &$v) {
                //遍历获取房间状态
                $where = [];
                $where[] = ['booking_time', ['EGT',$begintime],['ELT',$endtime],'and'];
                $where[] = [ 'room_id','=', $v['id']];
                $where[] = ['pay_status', '=', 1];
                $booking = $roomBookingModel->field('*')->where($where)->select();
                $v['type'] = 1;
                if(!$booking->isEmpty()){
                    if($booking[0]['to_shop'] == 1){
                        $v['type'] = 3;
                    }else if($booking[0]['to_shop'] == 2){
                        $v['type'] = 1;
                    }else{
                        $v['type'] = 2;
                    }
                }
            }
        }
        $result['data'] = $list;
        return $result;
    }

    public function details(){
        $room_id = Request::param('room_id', false);
        $roomModel = new RoomModel;
        $result = $roomModel->getOne($room_id);
        $settingModel = new SettingModel();
        $result['chongzhi'] = $settingModel->getValue('chongzhi_msg');
        $result['debug'] = $settingModel->getValue('debug');
        return $result;
    }

    public function  booking(){
        $user_info = model('user')->get($this->userId);
        if(!$user_info->isbuy) {
            return [
                'status' => false,
                'data' => '',
                'msg' => '用户被限制购买, 请联系客服',
            ];
        }
        $data['room_id'] = Request::param('room_id', false);
        $data['booking_time'] = Request::param('booking_time', false);
        $data['come_time'] = Request::param('come_time', false);
        $data['mobile'] = Request::param('mobile', false);
        $data['name'] = Request::param('name', false);
        $data['memo'] = Request::param('memo', false);
        $data['taocan'] = Request::param('taocan', '');
        $data['user_id'] = $this->userId;
        $data['type'] = 1;
        $roomBookingModel = new RoomBookingModel;

        return $roomBookingModel->createBooking($data);
    }

    public function getBookingList()
    {
        $model = new RoomBookingModel();
        return $model->getBookingList($this->userId,1,1000);
    }

    public function bookingDetails(){
        $id = input('id');
        $model = new RoomBookingModel();
        $where['id'] = $id;
        $result = $model->field('*')->where($where)->find();
        if($result)
        {
            $return_data = array(
                'status' => true,
                'msg' => '获取成功',
                'data' => $result
            );
        }
        else
        {
            $return_data = array(
                'status' => false,
                'msg' => '获取失败',
                'data' => $result
            );
        }
        return $return_data;
    }

    public function del(){
        $id = input('id');
        $user_id = $this->userId;
        $model = new RoomBookingModel();
        //$booking = $model->field('*')->where(['id' => $id])->find();
        //$booking['isdel'] = 1;
        $model->save(['isdel' => 1], ['id' => $id]);
        $return_data = array(
            'status' => true,
            'msg' => '删除成功',
            'data' => []
        );
        return $return_data;
    }
}